package com.ttw;

import android.app.Application;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;

import androidx.core.app.NotificationCompat;

import me.weishu.reflection.Reflection;

@KeepNotProguard
public  class TTWLocal {


    @KeepNotProguard
    public  interface DaemonConfiguration{
        NotificationCompat.Builder getIntentNotificationBuilder(Context context);
        Notification getForegroundNotification();
        Boolean isMusicPlayEnabled();
        Boolean isOnePixelActivityEnabled();
    }

    private  static DaemonConfiguration configuration =null;
    private  static Callback callback=null;
    private  static Application application=null;

    public static Callback getCallback(){return callback;}

    public static Application getApplication() {
        return application;
    }

    public static DaemonConfiguration getConfiguration() {
        return configuration;
    }

    public static boolean isMainProcess(Application app) {
        return ServiceUtils.isMainProcess(app);
    }

    public static void startActivity(Context context, Intent intent) {
        if (ActivityUtils.isAppRunningForeground(context)) {
            context.startActivity(intent);
            return;
        }
        ActivityUtils.hookJumpActivity(context, intent);
    }

    public static void startWork(Application application, DaemonConfiguration configuration, final Callback callback) {
        if (TTWLocal.application != null) {
            return;
        }
        Reflection.unseal(application);
        TTWLocal.callback = callback;
        TTWLocal.configuration = configuration;
        TTWLocal.application = application;
        ScreenReceiver.register(application);
        ScreenReceiver.addObserver(new ScreenReceiver.Observer() {
            @Override
            public void screenStatusChanged(Boolean screenON) {
                callback.onReceiver(screenON ? Callback.HappenEvent.ACTION_SCREEN_ON : Callback.HappenEvent.ACTION_SCREEN_OFF);
            }
        });
        Daemon.init(application.getBaseContext(), new Configs(new Configs.Config(
                "android.process.media", "com.daemon.DaemonService", "", ""
        ), new Configs.Config(
                application.getPackageName() + ":service", "com.daemon.CoreService", "", ""
        )));
        CoreService.start(application);
        DaemonJobService.scheduleService(application);
    }
}
